﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SpaceFight
{
    public class ModelManager : DrawableGameComponent
    {
        Player player;
        List<SpaceShip> spaceShips;
        Effect spaceEffect;

        //Stars
        Effect starEffect;
        Texture2D starTexture;
        ParticleStarSheet stars;
        Skybox skybox;

        Texture2D particleTexture;

        public ModelManager(Game game)
            : base(game)
        {
            spaceShips = new List<SpaceShip>();
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spaceEffect = Game.Content.Load<Effect>("Effects/Effect_Space");

            particleTexture = Game.Content.Load<Texture2D>("Textures/Particle");

            starTexture = Game.Content.Load<Texture2D>("Textures/Stars");
            starEffect = spaceEffect.Clone();
            starEffect.CurrentTechnique = starEffect.Techniques["Star"];
            starEffect.Parameters["Texture"].SetValue(particleTexture);         //for the round shape of the star

            stars = new ParticleStarSheet(GraphicsDevice, new Vector3(2000, 2000, -1900), 6000, starEffect, starTexture);

            player = new Player(Game.Content.Load<Model>("Models/cruiser"), new Vector3(100, 100, 0), 0, 0, 0, new Vector3(0, 0, 0f));
            spaceShips.Add(new SpaceShip(Game.Content.Load<Model>("Models/cruiser"), 0, 0, 0, new Vector3(120, 100, 0), new Vector3(0, 0, 0f)));
            spaceShips.Add(new SpaceShip(Game.Content.Load<Model>("Models/cruiser"), 0, 0, 0, new Vector3(80, 100, 0), new Vector3(0, 0, 0f)));
            skybox = new Skybox(player, ((Game1)Game).camera, Game.Content);
            base.LoadContent();
            //("Models/spaceship1")
        }

        public override void Update(GameTime gameTime)
        {
            player.Update(gameTime);

            foreach (SpaceShip spaceShip in spaceShips)
                spaceShip.Update();

            base.Update(gameTime);
        }


        public override void Draw(GameTime gameTime)
        {
            //stars.Draw(((Game1)Game).camera);
            GraphicsDevice.BlendState = BlendState.Opaque;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            GraphicsDevice.RasterizerState = RasterizerState.CullNone;

           // skybox.Draw(Game.GraphicsDevice);
            foreach (SpaceShip spaceShip in spaceShips)
                spaceShip.Draw(((Game1)Game).camera);


            player.Draw(((Game1)Game).camera);
            
            base.Draw(gameTime);
        }


        public Matrix GetPlayerWorldMatrix()
        {
            return player.GetWorld();
        }

    }
}
